import java.util.concurrent.ArrayBlockingQueue;

public class h_2_consumer implements Runnable {
    private ArrayBlockingQueue arrayBlockingQueue;
    private String name;
    public h_2_consumer(ArrayBlockingQueue arrayBlockingQueue,String name){
        this.arrayBlockingQueue=arrayBlockingQueue;
        this.name=name;
    }
    @Override
    public void run() {
        Thread.currentThread().setName(name);
        while(true){
            try {
                if(arrayBlockingQueue.size()>0){
                    arrayBlockingQueue.take();
                    System.out.println(Thread.currentThread().getName()+"吃了一个汉堡,现存"+arrayBlockingQueue.size()+"个");
                }
                else  {
                    System.out.println(Thread.currentThread().getName()+"没吃到，库存不够");
                    Thread.sleep(1000);
                    notify();
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}
